﻿using Infrastructure;
using Infrastructure.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DormMS.IService;
using DormMS.IService.Common;
using DormMS.Model;
using DormMS.Model.SearchModels;
using System.IO;

namespace DormMS.Controllers
{
    public class UserController : BaseController
    {
        protected IUserGroupService usergroupservice;
        protected ILogService logService;
        public UserController(IUserInfoService s, IUserGroupService _usergroupservice,ILogService l) : base(s)
        {
            usergroupservice = _usergroupservice;
            logService = l;
        }
        // GET: User
        [AuthorizeFilter(MenuRole = "4")]
        [HttpGet]
        public ActionResult UserGroupManage()
        {
            return View();
        }

        /// <summary>
        /// 分组管理分页数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "4")]
        [HttpPost]
        public ActionResult UserGroupManage(UserGroupSearchModel search)
        {
            int total = 0;
            var list = usergroupservice.UserGroupManage(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 新增、修改分组保存
        /// </summary>
        /// <param name="userGroup"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "4")]
        [HttpPost]
        public ActionResult UserGroupEdit(UserGroup userGroup)
        {
            var result = new Result();
            result = usergroupservice.UserGroupEdit(userGroup, CurrentLoginUser.Id);
            if (result.isSuccess)
            {
                if (userGroup.Id > 0)
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "编辑UserGroup", $"编辑Id为{userGroup.Id}的UserGroup成功", Generator.GetIP());
                }
                else
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "新增UserGroup", "操作成功", Generator.GetIP());
                }
                
            }
            else
            {
                if (userGroup.Id > 0)
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "编辑UserGroup", $"编辑Id为{userGroup.Id}的UserGroup失败，"+result.msg, Generator.GetIP());
                }
                else
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "新增UserGroup", "操作失败"+result.msg, Generator.GetIP());
                }
            }
            return Json(result);
        }

        /// <summary>
        /// 获取树状菜单并选中已有权限
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "4")]
        [HttpPost]
        public ActionResult GetMenuList(int groupId)
        {
            return Json(usergroupservice.GetMenuList(groupId));
        }

        /// <summary>
        /// 更新权限
        /// </summary>
        /// <param name="AuthList"></param>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "4")]
        [HttpPost]
        public ActionResult UpdateGroupRole(List<string> AuthList, int GroupId)
        {
            var result = new Result();
            result = usergroupservice.UpdateGroupRole(AuthList, GroupId, CurrentLoginUser.Id);
            if (result.isSuccess)
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "更新权限", $"更新GroupId为{GroupId}的权限成功", Generator.GetIP());
            }
            else
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "更新权限", $"更新GroupId为{GroupId}的权限失败，"+result.msg, Generator.GetIP());
            }
            return Json(result);
        }

        /// <summary>
        /// 删除分组
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "4")]
        [HttpPost]
        public ActionResult DelGroupRole(int Id)
        {
            var result = new Result();
            result = usergroupservice.DelGroupRole(Id, CurrentLoginUser.Id);
            if (result.isSuccess)
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "删除分组", $"删除Id为{Id}的分组成功", Generator.GetIP());
            }
            else
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "删除分组", $"删除Id为{Id}的分组失败，"+result.msg, Generator.GetIP());
            }
            return Json(result);
        }

        /// <summary>
        /// 获取当前用户菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetUserMenu()
        {
            return Json(usergroupservice.GetUserMenu(CurrentLoginUser.GroupId,CurrentLoginUser.IsAdmin));
        }

        /// <summary>
        /// 获取用户分组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetUserGroupList()
        {
            return Json(usergroupservice.GetUserGroupList());
        }

        /// <summary>
        /// 用户管理
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "5")]
        [HttpGet]
        public ActionResult UserManage()
        {
            return View();
        }

        /// <summary>
        /// 用户管理页面数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "5")]
        [HttpPost]
        public ActionResult UserManage(UserSearchModel search)
        {
            int total = 0;
            var list = _userService.GetUserList(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="NewPassword"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "5")]
        [HttpPost]
        public ActionResult UpdateUserPassWord(int Id, string NewPassword)
        {
            var result = _userService.UpdateUserPassWord(Id, NewPassword, CurrentLoginUser.Id);
            if (result.isSuccess)
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                           "修改密码", $"修改Id为{Id}的用户密码成功", Generator.GetIP());
            }
            else
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                          "修改密码", $"修改Id为{Id}的用户密码失败"+result.msg, Generator.GetIP());
            }

            return Json(result);
        }

        /// <summary>
        /// 删除员工
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "5")]
        [HttpPost]
        public ActionResult DelUser(int Id)
        {
            var result = _userService.DelUser(Id, CurrentLoginUser.Id);
            if (result.isSuccess)
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                          "删除用户", $"删除Id为{Id}的用户成功" , Generator.GetIP());
            }
            else
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                          "删除用户", $"删除Id为{Id}的用户失败" + result.msg, Generator.GetIP());
            }
            return Json(result);
        }

        /// <summary>
        /// 新增、编辑用户信息
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "5")]
        [HttpGet]
        public ActionResult UserEdit()
        {
            return View();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "5")]
        [HttpPost]
        public ActionResult UserEdit(int Id)
        {
            return Json(new { userInfo = _userService.GetUserInfoById(Id), groupList = usergroupservice.GetUserGroupList() });
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "5")]
        [HttpPost]
        public ActionResult UserEditSubmit(UserInfo user)
        {
            var result = _userService.UserEditSubmit(user, CurrentLoginUser.Id);
            if (result.isSuccess)
            {
                if (user.Id >0)
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                          "编辑用户", $"编辑Id为{user.Id}的用户成功", Generator.GetIP());
                }
                else
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                          "新增用户", $"新增用户名为{user.UserName}的用户成功", Generator.GetIP());
                }
            }
            else
            {
                if (user.Id > 0)
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                          "编辑用户", $"编辑Id为{user.Id}的用户失败，"+result.msg, Generator.GetIP());
                }
                else
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                          "新增用户", $"新增用户名为{user.UserName}的用户失败，"+result.msg , Generator.GetIP());
                }
            }
            return Json(result);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="UserState"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "5")]
        [HttpPost]
        public ActionResult ChangeUserState(int Id, int UserState)
        {
            return Json(_userService.ChangeUserState(Id, UserState, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 用户修改密码
        /// </summary>
        /// <param name="OldPassword"></param>
        /// <param name="NewPassword"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangePassword(string OldPassword, string NewPassword)
        {
            return Json(_userService.UpdateUserPassWord(CurrentLoginUser.Id, NewPassword, CurrentLoginUser.Id, OldPassword));
        }

        /// <summary>
        /// 数据导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public ActionResult UpLoadProcess(HttpPostedFileBase file)
        {
            string fileName = System.IO.Path.GetFileName(file.FileName);
            string filePhysicalPath = Server.MapPath("~/Images/" + fileName);
            string pic = "", error = "";
            bool result = false;
            try
            {
                file.SaveAs(filePhysicalPath);
                pic = "/Images/" + fileName;                
                result = true;
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return Json(new
            {
                pic = pic,
                filename = fileName,
                error = error,
                result = result
            });
        }
    }
}